package com.after.saas.file.utils;

import com.after.saas.file.constants.ModuleEnum;
import com.after.saas.file.domain.vo.ExcelDownloadVO;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;

/**
 * @Classname ExcelTemplateUtil
 * @Description TODO
 * @Date 2021/8/3 9:21
 * @Created by hjchen
 */

@Slf4j
@UtilityClass
public class ExcelTemplateUtil {

    public ExcelDownloadVO downloadExcel(String mouldCode) {
        ExcelDownloadVO downloadVO = new ExcelDownloadVO();
        downloadVO.setMouldCode(mouldCode);
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource(String.format("template/excel/%s", ModuleEnum.getClazzByCode(mouldCode).getModuleFile()));
        downloadVO.setFileName(resource.getFilename());
        try {
            downloadVO.setInputStream(resource.getInputStream());
        } catch (IOException e) {
            log.error("模板下载失败 {}", e.getMessage());
        }
        return downloadVO;
    }
}